package xyz.xuminghai.javafx_learn.scene.control;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.stage.Stage;

import java.util.Objects;

/**
 * 2023/1/12 21:54 星期四<br/>
 * <p>
 * 菜单控件
 *
 * @author xuMingHai
 */
public class MenuApp extends Application {

    /**
     * 菜单栏
     */
    private final MenuBar menuBar = new MenuBar();


    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() {
        MenuItem menuItem1 = new MenuItem("menuItem1",
                new ImageView(Objects.requireNonNull(getClass().getResource("menuItem1.jpg")).toExternalForm()));
        menuItem1.setOnAction(event -> System.out.println("menuItem1.setOnAction"));
        menuItem1.setAccelerator(new KeyCodeCombination(KeyCode.K, KeyCombination.ALT_DOWN));
        MenuItem menuItem2 = new MenuItem("menuItem2");
        MenuItem menuItem3 = new MenuItem("menuItem3");
        MenuItem menuItem4 = new MenuItem("menuItem4");

        Menu menu1 = new Menu("menu1");
        menu1.getItems().addAll(menuItem1, new SeparatorMenuItem(),
                menuItem2, new SeparatorMenuItem(), menuItem3);

        Menu menu2 = new Menu("menu2");
        ToggleGroup toggleGroup = new ToggleGroup();
        RadioMenuItem radioMenuItem1 = new RadioMenuItem("radioMenuItem1");
        RadioMenuItem radioMenuItem2 = new RadioMenuItem("radioMenuItem2");
        RadioMenuItem radioMenuItem3 = new RadioMenuItem("radioMenuItem3");
        toggleGroup.getToggles().addAll(radioMenuItem1, radioMenuItem2, radioMenuItem3);
        Menu radioMenu = new Menu("radioMenu");
        radioMenu.getItems().addAll(radioMenuItem1, new SeparatorMenuItem(),
                radioMenuItem2, new SeparatorMenuItem(), radioMenuItem3);
        menu2.getItems().addAll(menuItem4, radioMenu);

        Menu menu = new Menu("menu");
        menu.getItems().add(new MenuItem("menuItem"));
        Menu menu3 = new Menu("menu3");
        menu3.getItems().addAll(menu, new CheckMenuItem("CheckMenuItem"));


        menuBar.getMenus().addAll(menu1, menu2, menu3);
        menuBar.setContextMenu(new ContextMenu(radioMenu.getItems().toArray(new MenuItem[0])));
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(menuBar, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
